package com.google.code.virtualhockey.vhx.sds.fields

import com.google.code.virtualhockey.vhx.sds._
import com.google.appengine.api.datastore.Link

/**
 * Special text string with the attached intrinsic meaning of URL and a size limit of 2038 characters.
 */
class SLink( parent: STMetaData[_],
             name: String,
             dbName: Option[String] = None ) extends SField( parent, name, false, dbName ) {

  protected type TRawType = String
  protected type TGaeType = Link
  protected val _clsRaw = classOf[TRawType]
  protected val _clsGae = classOf[TGaeType]

  override protected def validate( value: Option[TRawType] ): Unit = {
    if ( value != None && value.get.length() > 2038 )
      Predef.error( "A String persisted as Link cannot be longer than " + 2038 + " characters." )
  }

  override protected[sds] def convertGaeToSds( value: TGaeType ): TRawType = {
    value.getValue
  }

  override protected[sds] def convertSdsToGae( value: TRawType ): TGaeType = {
    new Link( value )
  }
}
